package com.yx.shop.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yx.shop.entitly.Type;
import com.yx.shop.service.TypeService;
import com.yx.shop.service.impl.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname TypeController
 * @Description TODO:
 * @Date 2021/05/07 11:46
 * @Created by yx
 */
@WebServlet("/type")
public class TypeController extends BaseServlet {
    private TypeService typeService = new TypeServiceImpl();

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/07 11:44
     * @Description:获取货物品类
     **/
    public String goodsType(HttpServletRequest request, HttpServletResponse response){
        String typeString = null;
        try {
            List<Type> typeList = typeService.getAllType();
            ObjectMapper objectMapper = new ObjectMapper();
            //将获取集合封装未json数据并发送
            typeString = objectMapper.writeValueAsString(typeList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return typeString;
    }
}
